package com.javasm.adminapi.aftersales.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.aftersales.service.B2cMagService;
import com.javasm.adminapi.common.R;
import com.javasm.adminapi.common.utils.ExportUtil;
import com.javasm.adminapi.module.aftersalesbase.entity.B2cReturnMsg;
import com.javasm.adminapi.module.aftersalesbase.entity.OmOrder;
import com.javasm.adminapi.module.aftersalesbase.service.B2cReturnMsgService;
import com.javasm.adminapi.module.aftersalesbase.service.OmOrderService;
import com.javasm.adminapi.module.aftersalesbase.vo.FormVo;
import com.javasm.adminapi.module.aftersalesbase.vo.ReturnMagSearchVo;
import com.javasm.adminapi.module.product.entity.TSpu;
import com.javasm.adminapi.module.product.service.TSpuService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @className: ReturnMagController
 * @description:
 * @author: zjl
 * @date: 2025/10/16 21:22
 * @version: 0.1
 * @since: jdk17
 */

@RestController
@RequestMapping("/mag")
public class B2cMagController {

    @Resource
    B2cMagService b2cMagService;

    @Resource
    B2cReturnMsgService b2cMag;
    @Resource
    ExportUtil<B2cReturnMsg> exportUtil;

    @Resource
    OmOrderService omOrderService;

    @Resource
    TSpuService tSpuService ;
    @GetMapping("/query")
    public R queryByCondition(ReturnMagSearchVo searchVo){
        PageInfo<B2cReturnMsg> pageInfo =b2cMagService.queryByCondition(searchVo);
        return R.ok(pageInfo);
    }

    @GetMapping("/query/list")
    public R queryById( Integer id){
        B2cReturnMsg b2cReturnMsg = b2cMagService.getById(id);
        return R.ok(b2cReturnMsg);
    }

    @PostMapping("/save/update")
    public R listById(@RequestBody B2cReturnMsg b2cReturnMsg){
        b2cMagService.saveOrUpdate(b2cReturnMsg);
        return R.ok();
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Integer[] ids){
        b2cMagService.removeByIds(ids);
        return R.ok();
    }

    @GetMapping("/export/excel")
    public R exportExcel(ReturnMagSearchVo searchVo){
        List<B2cReturnMsg> exportList =b2cMagService.listByCondition(searchVo);
        exportUtil.exportExcel(exportList,B2cReturnMsg.class);
        return R.ok();
    }


    @PostMapping("import/excel")
    public R importExcel(MultipartFile file) throws Exception {
        EasyExcel.read(file.getInputStream(),
                B2cReturnMsg.class,
                new PageReadListener<B2cReturnMsg>(dataList ->
                        b2cMag.saveBatch(dataList))
                ).sheet().doRead();
        return R.ok();
    }


    @GetMapping("/order/list")
    public R listOrderByCondition(FormVo searchVo){
        List<OmOrder> list = b2cMagService.listOrderByCondition(searchVo);
        return R.ok(list);
    }

    @GetMapping("/order/listAll")
    public R listOrder(){
        return R.ok(omOrderService.list());
    }



    @GetMapping("/tspu/list")
    public R listTspuByCondition(FormVo searchVo){
        List<TSpu> list = b2cMagService.listTspuByCondition(searchVo);
        return R.ok(list);
    }

    @GetMapping("/tspu/listAll")
    public R listTspu(){
        return R.ok(tSpuService.list());
    }

}
